#include "tileset.h"

tileMem_t tileSet[MAX_TILE_PER_TILESET];

bool init_tileset()
{
	for(int i=0; i < MAX_TILE_PER_TILESET; i++)
	{
		tileSet[i].tileImgData_p = NULL;
                tileSet[i].tileInfo.isValid = false;
		tileSet[i].tileRender.tilePic = NULL;
	}
	return true;
}

bool build_tileset_from_file(QString pathAndName)
{
	//open tileset file
	FILE *fp = NULL;
	fp=fopen(pathAndName.toAscii().data(),"rb+");
	if(NULL==fp)
	{
		printf("tileSet Error:open file failed!\n");
		return false;
	}
	//read tileset Entry.
	for(int i =0; i< MAX_TILE_PER_TILESET; i++)
	{
		if(1!=fread(&(tileSet[i].tileInfo),sizeof(tileInfo_t),1,fp))
		{
                        printf("tileSet Error:read Entry failed!");
			return false;
		}
	}

	//read img data
	for(int i =0; i< MAX_TILE_PER_TILESET; i++)
	{
		if(true == tileSet[i].tileInfo.isValid)
		{
			tileSet[i].tileImgData_p = (unsigned char*)malloc(tileSet[i].tileInfo.dataSize);
			if(1!=fread(tileSet[i].tileImgData_p, tileSet[i].tileInfo.dataSize, 1, fp))
			{
				printf("tilSet Error:read ImgData failed!");
				return false;
			}
                        tileSet[i].tileRender.tilePic = new QPixmap();
			tileSet[i].tileRender.tilePic->loadFromData(tileSet[i].tileImgData_p, tileSet[i].tileInfo.dataSize);
		}
	}
	//close file
	fclose(fp);
	return true;
}



bool add_tile_into_tileset(QString pathAndName, int tileId)
{
	tileMem_t *tile = &(tileSet[tileId]);
	int tileSize;
        if(false == tile->tileInfo.isValid)
	{
		//open tile file
		FILE *fp = NULL;
		fp=fopen(pathAndName.toAscii().data(),"rb+");
		if(NULL==fp)
		{
			printf("tile Error:open file failed!\n");
			return false;
		}
		tileSize = com_cac_file_size(fp);
		tile->tileImgData_p = (unsigned char*)malloc(tileSize);
                if(1!=fread(tile->tileImgData_p,tileSize,1,fp))
		{
                        printf("tile Error:read file failed!");
			return false;
		}
		//close file
		fclose(fp);

		tile->tileInfo.isValid = true;
                tile->tileRender.tilePic = new QPixmap();
		tile->tileRender.tilePic->loadFromData(tile->tileImgData_p, tileSize);
		//update Entry	
		tile->tileInfo.dataSize = tileSize;
		tile->tileInfo.width = tile->tileRender.tilePic->width();
		tile->tileInfo.height = tile->tileRender.tilePic->height();
	}
	else
	{
		printf("Cannot add into an existed tile:%d\n",tileId);
		return false;
	}
	return true;
}

bool remove_tile_from_tileset(int tileId)
{
	tileMem_t *tile = &(tileSet[tileId]);
	if(true == tile->tileInfo.isValid)
	{
                tile->tileInfo.isValid= false;
		if(tile->tileRender.tilePic)
		{
			delete tile->tileRender.tilePic;
			tile->tileRender.tilePic = NULL;
		}
		if(tile->tileImgData_p)
		{
			free(tile->tileImgData_p);
			tile->tileImgData_p = NULL;
		}
	}
	else
	{
		printf("Cannot remove an empty tile:%d\n",tileId);
		return false;
	}
	return true;
}


bool save_tileset_to_file(QString pathAndName)
{
	FILE *fp = NULL;
	fp=fopen(pathAndName.toAscii().data(),"wb+");
	if(NULL!=fp)
	{
		//write tileset Entry.
		for(int i =0; i< MAX_TILE_PER_TILESET; i++)
		{
			if(1!=fwrite(&(tileSet[i].tileInfo),sizeof(tileInfo_t),1,fp))
			{
                                printf("tileSet Entry Error:write file failed!");
				return false;
			}
		}
		//write image data
                for(int i =0; i< MAX_TILE_PER_TILESET; i++)
		{
			if(true == tileSet[i].tileInfo.isValid)
			{
				if(1!=fwrite(tileSet[i].tileImgData_p,tileSet[i].tileInfo.dataSize, 1, fp))
				{
                                        printf("tileSet Imgdata Error:write file failed!");
					return false;
				}
			}
		}
		fclose(fp);
	}
	else
	{
		printf("Tileset Savefile Error:open file failed");
		return false;
	}
	return true;
}

bool release_tileset()
{
	for(int i=0; i< MAX_TILE_PER_TILESET; i++)
	{
		if(true==tileSet[i].tileInfo.isValid)
		{
		   remove_tile_from_tileset(i);
		}
	}
	return true;
}


